Ext.define('planner.controller.Resources', {
	extend: 'Ext.app.Controller',
	views: [
		'developer.Developers',
		'Jobs.Jobs',
		'Jobs.JobsGraph',
		'developer.Developer',
		'Jobs.Job',
		'Requeres.RequeresForum',
		'toolbars.Trash',
		'toolbars.MainToolBar',
	],
	requires: [
		//'diagramms.Developers',
	],
	linkDeveloperToJob: function(view, developer, job) {
		Ext.Ajax.request({
		    url: 'data/link_developer_with_job/',
		    params: {
		        developer_id: developer.get('id'),
		        job_id: job.get('id'),
		    },
		    success: function(response) {
		        var text = response.responseText;
		        // process server response here
		        view.getStore().load();
		        console.log(text);
		    }
		});
	},
	linkDeveloperToParent: function(view, parent, developer) {
		Ext.Ajax.request({
		    url: 'data/link_developer_with_parent/',
		    params: {
		        developer_id: developer.get('id'),
		        parent_id: parent.get('id'),
		    },
		    success: function(response) {
		        var text = response.responseText;
		        // process server response here
		        view.getStore().load();
		        console.log(text);
		    }
		});
	},
	linkJobToParent: function(view, parent, job) {
		var store = Ext.getStore('Jobs');
		Ext.Ajax.request({
		    url: 'data/link_job_with_parent/',
		    params: {
		        job_id: job.get('id'),
		        parent_id: parent.get('id'),
		    },
		    success: function(response) {
		        var text = response.responseText;
		        // process server response here
		        //store.load();
		        console.log(text);
		    }
		});
	},
	init: function() {
		var me = this;
		Ext.Loader.config.disableCaching = false;
		this.control({
			'MainToolBar': {
				addJobEvent: function() {
					console.log('Resources: addJobEvent');
                	var store = Ext.getStore('Jobs');
                	store.add({name: 'noname', type: 'job'});
				},
			},
			'RequeresForum': {
				addRequireEvent: function(parentId, text) {
					console.log('Resources: RequeresForum.addRequireEvent: ' + text  + ' in id=' + parentId);
                	var store = Ext.getStore('Requeres');
                	var node = store.getNodeById(parentId)
                	var parentLevel = node.get('level')
                	console.log('level=' + parentLevel)
                	node.set('leaf', false)
                	node.appendChild(node.createNode({
                		parent: parentId, 
                		date: 1000000, 
                		source: 'asd', 
                		source_id: 1, 
                		state: 'new', 
                		title: text, 
                		body: text, 
                		level: parentLevel + 1
                	}));
				},
			},
			'Developers': {
				afterrender: function(view) {
					console.log('Resources: Developers afterrender');
				},
				itemdblclick: function(view, record) {
					console.log('Resources: Developers itemdblclick, name=' + record.get('name') + ', jobs=' + record.get('jobs'));
					var view = Ext.widget('Developer');
					view.down('form').loadRecord(record);
				},
				linkDeveloperToJob: function(view, developer, job) {
					me.linkDeveloperToJob(view, developer, job);
				},
				linkDeveloperToParent: function(view, developer, parent) {
					me.linkDeveloperToParent(view, developer, parent);
				},
			},
			'JobsGraph': {
				afterrender: function(view) {
					console.log('Resources: JobsGraph afterrender');
				},
				removeJobEvent: function(job) {
					console.log('Resources: removeJobEvent');
                	var store = Ext.getStore('Jobs');
                	store.remove(job);
				},
				updateJobEvent: function(job, newValue) {
					console.log('Resources: updateJobEvent');

                	job.beginEdit();
                	job.set('name', newValue);
                	job.endEdit();
				},
				linkJobToParent: function(view, job, parent) {
					me.linkJobToParent(view, job, parent);
				},
			},
			'Jobs': {
				afterrender: function(view) {
					console.log('Resources: Jobs afterrender');
				},
				itemdblclick: function(view, record) {
					console.log('Resources: Jobs itemdblclick, name=' + record.get('name'));
					var view = Ext.widget('Job');
					view.down('form').loadRecord(record);
				},
				linkDeveloperToJob: function(view, developer, job) {
					me.linkDeveloperToJob(view, developer, job);
				},
				linkJobToParent: function(view, job, parent) {
					me.linkJobToParent(view, job, parent);
				},
			},
			'Developer button[action=save]': {
                click: function(button) {
                	var win    = button.up('window'),
                	form   = win.down('form'),
                	record = form.getRecord(),
                	values = form.getValues();

                	record.set(values);
                	win.close();
                }
            },
			'Job button[action=save]': {
                click: function(button) {
                	var win    = button.up('window'),
                	form   = win.down('form'),
                	record = form.getRecord(),
                	values = form.getValues();

                	record.set(values);
                	win.close();
                }
            },
			'Trash': {
                removeJob: function(trash, job) {
                	console.log('Resources: Trash removeJob=' + job.get('name'));
                	var store = Ext.getStore('Jobs');
                	store.remove(job);
                },
                removeDeveloper: function(trash, developer) {
                	console.log('Resources: Trash removeDeveloper=' + developer.get('name'));
                	var store = Ext.getStore('Developers');
                	store.remove(developer);
                },
            },
		});
	}
});
